package controller;

import model.Users;

import service.ServiceException;
import service.UserService;

public class AuthentificationController {
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	 /**
     * This method authenticates a user
     * 
     * @param username
     *            the username
     * @param pasword
     *            the password
     * @throws ControllerException
     * @throws IllegalStateException
     *             if a user is active and currently logged in
     */
    public Users login(String username, String password) throws ControllerException {
        try {
            Users user = userService.getUserByUsername(username);
            if (userService.verifyPassword(user, password)) {
                return user;
            } else {
                throw new ControllerException("Incorrect login!");
            }
        } catch (ServiceException e) {
            throw new ControllerException(e.getMessage(), e);
        }
    }
}
